//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2004
//
// Dialog for editing number.
//

# include "StdAfx.h"
# include "Resource.h"
# include "EditNumber.h"
# include "Globals.h"

struct edit_data_t {
	int * piNumber;
	const char * strComment;
	bool bSigned;
};

static
INT_PTR CALLBACK EditProc (HWND _hWnd, UINT _uMsg, WPARAM _wParam, LPARAM _lParam) {
	switch (_uMsg) {
		case WM_INITDIALOG: {
			edit_data_t * const pData = reinterpret_cast<edit_data_t *> (_lParam);
			::SetWindowLong (_hWnd, GWL_USERDATA, _lParam);

			::SetDlgItemText (_hWnd, IDC_COMMENT, pData->strComment);
			::SetDlgItemInt (_hWnd, IDC_NUMBER, * pData->piNumber, pData->bSigned);

			::EnableWindow (::GetDlgItem (_hWnd, IDOK), FALSE);

			return TRUE;
		}

		case WM_COMMAND: {
			switch (LOWORD (_wParam)) {
				case IDC_NUMBER: {
					if (HIWORD (_wParam) == EN_CHANGE) {
						const edit_data_t * const pData = reinterpret_cast<edit_data_t *> (::GetWindowLong (_hWnd, GWL_USERDATA));

						BOOL bTranslated = FALSE;
						::GetDlgItemInt (_hWnd, IDC_NUMBER, & bTranslated, pData->bSigned);

						::EnableWindow (::GetDlgItem (_hWnd, IDOK), bTranslated);
					}
					return TRUE;
				}

				case IDOK: {
					edit_data_t * const pData = reinterpret_cast<edit_data_t *> (::GetWindowLong (_hWnd, GWL_USERDATA));

					BOOL bTranslated = FALSE;
					* pData->piNumber = ::GetDlgItemInt (_hWnd, IDC_NUMBER, & bTranslated, pData->bSigned);

					::EndDialog (_hWnd, 0);

					return TRUE;
				}

				case IDCANCEL:
					::EndDialog (_hWnd, 1);
					return TRUE;
			}
		}
	}

	return FALSE;
}

bool EditNumber (HWND _hWnd, int & _iNumber, const char * _strComment, bool _bSigned) {
	edit_data_t data = {& _iNumber, _strComment, _bSigned};
	return DlgBox (IDD_EDIT_NUMBER, _hWnd, EditProc, reinterpret_cast<LPARAM> (& data)) == 0;
}
